package com.changan.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @program: springboot-mp
 * @description: MyBatisPlus的配置类
 * @author: Mr.Nie
 * @create: 2022-01-07 14:53
 **/
@Slf4j
@Configuration
public class MyBatisPlusConfig implements MetaObjectHandler {

    /**
     * 功能描述: <br>
     * 〈增加的时候执行的填充〉
     * @Param: [metaObject]
     * @Return: void
     * @Author: Mr.Nie
     * @Date: 2022/1/7 14:56
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(),metaObject);
        this.setFieldValByName( "updateTime", new Date(),metaObject);
    }

    /**
     * 功能描述: <br>
     * 〈修改的时候的填充〉
     * @Param: [metaObject]
     * @Return: void
     * @Author: Mr.Nie
     * @Date: 2022/1/7 14:57
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    /**
     * 功能描述: <br>
     * 〈乐观锁配置〉
     * @Param: []
     * @Return: com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
     * @Author: Mr.Nie
     * @Date: 2022/1/7 15:32
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

